/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Message.Message;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


/**
 *
 * @author CUCAINHO
 */
class  ReciveClient extends GUI_Main implements Runnable { 
	
	private Socket theclient;  
	private ObjectInputStream in;
	public ReciveClient (Socket theclient) { 
		this.theclient = theclient; 		
		Thread t = new Thread(this); 
		t.start(); 
	} 
        
        public static void SendToClient(Socket theclient) throws IOException{           
             
            ObjectOutputStream out = new ObjectOutputStream(theclient.getOutputStream());
            System.out.println("Send Client! ");
            Object obj = new String("Server!!!");
            Message mssend = new Message(1,1,obj);
            out.writeObject(mssend);
        }
        
        public static void SendRequestViewDesktop(Socket theclient) throws IOException{ 
	 ObjectOutputStream out = new ObjectOutputStream(theclient.getOutputStream());
            System.out.println("Send Request view desktop ");
            Object obj = new String("Server!!!");
            Message mssend = new Message(5,1,obj);
            out.writeObject(mssend);
            drawGUIView();
            
        }
	public void run() {             
                
            int k = 0;
		while(true){
                    System.out.println("nghe lan thu: " + k);
              
		try 
		{  
                     Message ms = new Message();
                  
                  try{
                        in = new ObjectInputStream(theclient.getInputStream());
                       
                  }
                  catch(Exception ex){
                      System.out.println("Loi new input stream" + ex.toString());
                  }
                try {
                       ms = (Message)in.readObject(); // doc message                  
                    } catch (Exception ex) {
                        System.out.println("Loi khi doc message." + ex.toString());
                        ex.printStackTrace();
              
                    }
                        
                   
                        if(ms != null) {
                            try
                            {                                    
                                HandMessage(ms ,theclient );				
                            }catch(Exception ex){
                                System.out.println("message null!");

                            }                                    
                              
			} 
                k++;               
		} 
		catch (Exception e) { 

		}
                
                k++;
	} 
        }
	
}
